package {
	import ypmits.pixelbender.PBJFilter;
	import ypmits.unittest.BaseTest;

	import flash.display.Shader;
	import flash.display.ShaderPrecision;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	/**
	 * @author Dave Lenz
	 */
	public class PBJTest extends BaseTest {
		
		[Embed(source="twirl.pbj", mimeType="application/octet-stream")]
		private var twirlfilter : Class;

		private var twirl : PBJFilter;
		private var s : Sprite;
		
		public function PBJTest() {
			super( 0, false );

			addEventListener( Event.ADDED_TO_STAGE, init );
		}
		/*

		 */

		
		private function init( e : Event ) : void {
			removeEventListener( Event.ADDED_TO_STAGE, init );

			s = new Sprite();
			addChild( s );

			redraw( );

			twirl = new PBJFilter( new Shader( new twirlfilter() ), null, ShaderPrecision.FULL );
			twirl.setParameter("radius", 50 );
			twirl.setParameter("center", [ sw * .5, sh * .5 ] );
			twirl.setParameter("twirlAngle", 104 );
			s.filters = [ twirl.shaderfilter ];
			
			stage.addEventListener( MouseEvent.MOUSE_MOVE, mouseHandler );
			mouseHandler( null );
			resizeHandler( null );
		}

		
		private function mouseHandler( e : MouseEvent ) : void {
			twirl.setParameter("radius", mouseX > 50 ? mouseX * .5 : 50 );
			twirl.setParameter("twirlAngle", mouseY > 50 ? mouseY * .5 : 50 );
			s.filters = [ twirl.shaderfilter ];
		}

		
		private function redraw() : void {
			s.graphics.clear();
			
			var i : int = 0;
			for (; i < stage.stageWidth; i++) {
				if( i%2 ) {
					s.graphics.lineStyle( 1, 0x000000, 1, true );
					s.graphics.moveTo( i, 0 );
					s.graphics.lineTo( i, stage.stageHeight );
				}
			}
		}

		
		override protected function resizeHandler( e : Event ) : void {
			super.resizeHandler( e );
			
			redraw( );
			twirl.setParameter("center", [ sw * .5, sh * .5 ] );
			s.filters = [ twirl.shaderfilter ];
		}
	}
}
